<?

define('FPDF_FONTPATH','/usr/share/fpdf/font/');
include("/usr/share/fpdf/fpdf.php");

$d1 = date('m/d/Y');
$d2 = date('M. Y');
function Int2Text($num,$opt=null) {
                if ($opt == null)
                $opt=array();
                $transnum=array(
                                "."        => "% AND %",
                                "0"        => "ZERO",
                                "1"        => "ONE",
                                "2"        => "TWO",
                                "3"        => "THREE",
                                "4"        => "FOUR",
                                "5"        => "FIVE",
                                "6"        => "SIX",
                                "7"        => "SEVEN",
                                "8"        => "EIGHT",
                                "9"        => "NINE",
                                "10"        => "TEN",
                                "11"        => "ELEVEN",
                                "12"        => "TWELVE",
                                "13"        => "THIRTEEN",
                                "14"        => "FOURTEEN",
                                "15"        => "FIFTEEN",
                                "16"        => "SIXTEEN",
                                "17"        => "SEVENTEEN",
                                "18"        => "EIGHTEEN",
                                "19"        => "NINETEEN",
                                "20"        => "TWENTY",
                                "20+"        => "TWENTY-%",
                                "30"        => "THIRTY",
                                "30+"        => "THIRTY-%",
                                "40"        => "FOURTY",
                                "40+"        => "FOURTY-%",
                                "50"        => "FIFTY",
                                "50+"        => "FIFTY-%",
                                "60"        => "SIXTY",
                                "60+"        => "SIXTY-%",
                                "70"        => "SEVENTY",
                                "70+"        => "SEVENTY-%",
                                "80"        => "EIGHTY",
                                "80+"        => "EIGHTY-%",
                                "90"        => "NINETY",
                                "90+"        => "NINETY-%",
                                "100"        => "ONE-HUNDERED",
                                "100+"        => "ONE-HUNDERED-%",
                                "*100"        => "% HUNDERED",
                                "*100+"        => "%-HUNDERED-%",
                                "1000"        => "ONE-THOUSAND",
                                "1000+"        => "ONE-THOUSAND %",
                                "*1000"        => "%-THOUSAND",
                                "*1000+"        => "%-THOUSAND %",
                                );
                /* String to return */
                $str="";
                /* Debug mode? */
                $debug=$opt["debug"];
                if ($debug)        echo "<TABLE BORDER=\"1\"><TH>Int2Text(".$num.")</TH><TR><TD>";

                /* Trash all spaces and tabs, strip leading 0 */
                $num=str_replace(" ","",$num);
                $num=str_replace("\t","",$num);
                while($num[0]=="0") {
                        $num=substr($num,1);
                        if ($opt["no-strip-lead-0"]) {
                                if (strlen($str))
                                        $str.=" ";
                                $str.=$transnum["0"];
                        }
                }
                if (!strlen($num))
                        $num="0";

                /* Negative numbers */
                $anum=explode(".",$num);
                if ($num[0]=="-") {
                        if ($fmt=$transnum["-"]) {
                                $leftstr=Int2Text(substr($num,1),$opt);
                                if ($debug)        echo "negative: printf('".$fmt."','".$leftstr."')"."<br />";
                                $res=sprintf(Int2Textfmt($fmt),$leftstr);
                                if (strlen($str))        $str.=" ";
                                $str.=$res;
                                $curr=0;
                        }
                }
                /* float numbers */
                else if (count($anum)==2) {
                        if ($fmt=$transnum["."]) {
                                $leftstr=Int2Text($anum[0],$opt);
                                $rightstr=$anum[1].'/100';
                                if ($debug)        echo "float: printf('".$fmt."','".$leftstr."','".$rightstr."')"."<br />";
                                $res=sprintf(Int2Textfmt($fmt),$leftstr,$rightstr);
                                if (strlen($str))        $str.=" ";
                                $str.=$res;
                                $curr=0;
                        }

                /* Regular numbers */
                } else {
                        /* Higher N^10-number we can test with $num (1024 => 1000) */
                        $higher="1".my_str_repeat("0",max(0,strlen($num)-1));

                        /* Current (de)composed number */
                        $curr=$num;

                        /* Loop */
                        do {
                                /* Add a space (22 => 'twenty<space>two') */
                                if ($str)
                                        $str.=" ";
                                /* Strip leading 0 */
                                while($curr[0]=="0")
                                        $curr=substr($curr,1);
                                if (!strlen($curr))
                                        $curr="0";

                                /* Direct number - example: 5 => 'five' */
                                if ($res=$transnum[$curr]) {
                                        if ($debug)        echo "direct: printf('".$res."')<br />";
                                        $curr="";
                                        $str.=$res;
                                /* Composed number - example: 124 */
                                } else if (strlen($higher)>0) {

                                        /* Try to divide $curr using $higher and get left/right members */
                                        /* If this fails, we'll try again in next loop using $higher/10 */
                                        $left=substr($curr,0,max(0,strlen($curr)-strlen($higher)+1));
                                        $right=substr($curr,max(0,strlen($curr)-strlen($higher)+1));
                                        while($right[0]=="0")
                                                $right=substr($right,1);
                                        if (!strlen($right))
                                                $right="0";
                                        /* Zeros of $higher (1000 => '000') */
                                        $highzero=substr($higher,1);
                                        if ($debug)        echo "try-to-decompose: ".$curr."=".$higher."*".$left."+".$right.":<br />";

                                        /* Left member is not null */
                                        if ($left>0) {

                                                /* B+S - Example: 101 (100+1, B=100 S=1) => 'One hundred and one' */
                                                if ($fmt=$transnum[$left.$highzero."+"]) {
                                                        $leftstr=Int2Text($right,$opt);
                                                        if ($debug)        echo "B+S: printf('".$fmt."','".$leftstr."')"."<br />";
                                                        $res=sprintf(Int2Textfmt($fmt),$leftstr);
                                                        $str.=$res;
                                                        $curr="";
                                                }
                                                /* M*B+S - Example: 207 (2*100+1, M=2 B=100 S=7) => 'Two hundred and seven' */
                                                else if (($right!="0") && ($fmt=$transnum["*".$higher."+"])) {
                                                        $leftstr=Int2Text($left,$opt);
                                                        $rightstr=Int2Text($right,$opt);
                                                        if ($debug)        echo "M*B+S: printf('".$fmt."','".$leftstr."','".$rightstr."')"."<br />";
                                                        $res=sprintf(Int2Textfmt($fmt),$leftstr,$rightstr);
                                                        $str.=$res;
                                                        $curr="";
                                                }
                                                /* M*B - Example: 300 (3*100, M=3 B=100) => 'Three hundred' */
                                                else if ($fmt=$transnum["*".$higher]) {
                                                        $leftstr=Int2Text($left,$opt);
                                                        if ($debug)        echo "M*B: printf('".$fmt."','".$leftstr."')"."<br />";
                                                        $res=sprintf(Int2Textfmt($fmt),$leftstr);
                                                        $str.=$res;
                                                        $curr=$right;
                                                }
                                                /* B+S2, where HIGHER < S2 < 9*HIGHER  - Example (french): 72 => 'Soixante douze' */
                                                else {
                                                        for($low=9;$low>0;$low--) {
                                                                if ($curr) {
                                                                        if (strlen($curr) >= strlen($higher)) {
                                                                                if ($left > $low) {
                                                                                        if ($fmt=$transnum[$low.$highzero."++"]) {
                                                                                                $leftstr=Int2Text($right+$higher*($left-$low),$opt);
                                                                                                if ($debug)        echo "B+S2: printf('".$fmt."','".$leftstr."')"."<br />";
                                                                                                $res=sprintf(Int2Textfmt($fmt),$leftstr);
                                                                                                $str.=$res;
                                                                                                $curr="";
                                                                                        }
                                                                                }
                                                                        }
                                                                }
                                                        }
                                                        if ($debug) {
                                                                if ($curr)        echo "failed<br />";
                                                        }
                                                }
                                        }

                                        /* Next try: $higher/10 */
                                        $higher=substr($higher,0,strlen($higher)-1);

                                /* Oops - we couldn't decompose successfully - should never happend! */
                                } else {
                                        echo "fatal for '".$num."' (".$curr.")<br />";
                                        return $num;
                                }

                                /* Loop until curr==0 (decomposed) */
                                if ($curr == "0")
                                        $curr="";

                        } while (strlen($curr));

                }

                /* Return the result */
                if ($debug)        echo "</TD></TR></TABLE>\n";
                return $str;
        }

        /* Change '%' into '%s' for internal purpose */
        function Int2Textfmt($str) {
                return str_replace("%","%s",$str);
        }
        function my_str_repeat($str,$rep) {
                $tmp="";
                for($i=0;$i<=$rep;$i++)
                        $tmp.=$str;
                return $tmp;
        }   

$account = array(
		'name'=>'Snapy Comunications Inc.',
		'name2'=>'',
		'address1'=>'1500 Univercity Drive, ste 117',
		'address2'=>'',
		'city'=>'Coral Springs',
		'state'=>'FL',
		'zip'=>'33071',
		'country'=>'',
		'holder_logo'=>'./logos/Skull.gif',	
		'bank_name'=>'Bank of America',
		'bank_address'=>'1000 North Federal Highway',
		'bank_address2'=>'',
		'bank_city'=>'Boca Raton',
		'bank_state'=>'FL',
		'bank_zip'=>'32432',
		'bank_country'=>'',
		'bank_logo'=>'./logos/voziloto.gif',	
		'routing_number'=>'&063000047&',
		'account_number'=>"003679595765/",
	);
$check = array(
		'first_name'=>'Brian',
		'last_name'=>'Poulsen',
		'address1'=>'1365 Renfrew',
		'address2'=>'',
		'city'=>'Vancouver',
		'state'=>'BC',
		'zip'=>'V5M 4C3',
		'country'=>'Canada',
		'check_number'=>1603,
		'amount'=>number_format(18835.40, 2, '.', ''),	
		'memo'=>'resellerspanel.com'
	);
for($i=0; $i<5; $i++){
$checks_array[$i]=$check;
}


function generate_check($pdf, $account, $check){
	global $d1;
	global $d2;
	$check_sum_number='**'.number_format($check['amount'],2);
	$check_sum_word=Int2Text($check['amount']);
	$bank_line_code=$account['routing_number']." ".$account['account_number']." ".$check['check_number'];
	$s = 10;
	/*	
	pdf_set_parameter($pdf,'FontOutline','Arial=./fonts/arial.ttf');
	$font1 = pdf_findfont($pdf, 'Arial','host', 0);
	pdf_set_parameter($pdf,'FontOutline','Arial-Bold=./fonts/arialbd.ttf');
	$font2 = pdf_findfont($pdf, 'Arial-Bold','host', 0);
	pdf_set_parameter($pdf,'FontOutline','Time New Roman=./fonts/times.ttf');
	$font3 = pdf_findfont($pdf, 'Time New Roman' ,'host', 0);
	pdf_set_parameter($pdf,'FontOutline','Time New Roman-Bold=./fonts/timesbd.ttf');
	$font4 = pdf_findfont($pdf, 'Time New Roman-Bold' ,'host', 0);
	pdf_set_parameter($pdf,'FontOutline','Time New Roman-Bold Italic=./fonts/timesbi.ttf');
	$font5 = pdf_findfont($pdf, 'Time New Roman-Bold Italic' ,'host', 0);
	pdf_set_parameter($pdf,'FontOutline','Code=./fonts/MIPSMICR.TTF');
	$font6 = pdf_findfont($pdf, 'Code' ,'host', 0);
	*/
	$pdf->Line( 495, 724, 588, 724);
	//pdf_stroke($pdf);
	$pdf->Line( 78, 688, 480, 688);
	//pdf_stroke($pdf);
	$pdf->Rect( 495, 687, 93, 17);
        //pdf_stroke($pdf);
	$pdf->Line( 26, 663, 535, 663);
	//pdf_stroke($pdf);
	$pdf->Line( 61, 583 , 298, 583);
	//pdf_stroke($pdf);
	$pdf->Line( 379, 583, 590, 583);
	//pdf_stroke($pdf);
	if($account['holder_logo']){
		$im1 = pdf_open_image_file($pdf, 'gif', $account['holder_logo'],"",0);
		pdf_place_image($pdf, $im1, 19, 777-pdf_get_value($pdf, "imageheight", $im1), 1.0);
		pdf_close_image($pdf, $im1);
	}
	if($account['bank_logo']){
		$im2 = pdf_open_image_file($pdf, 'gif', $account['bank_logo'],"",0);
		pdf_place_image($pdf, $im2, 255, 777-pdf_get_value($pdf, "imageheight", $im2), 1.0);
		pdf_close_image($pdf, $im2);
	}
        $pdf->SetFont('Arial');
        pdf_show_xy($pdf, 'DATE', 462, 727);
        pdf_show_xy($pdf, 'PAY TO THE', 24, 702);
        pdf_show_xy($pdf, 'ORDER OF', 24, 691);
        pdf_show_xy($pdf, 'DOLLARS', 540, 666);
        pdf_show_xy($pdf, 'MEMO', 23, 584);	
        
        pdf_setfont($pdf, $font2, $s+2);
        pdf_show_xy($pdf, $account['name1'], intval((216-pdf_stringwidth($pdf, $account['name1'], $font2, $s+2))/2)+53, $h=765);
        if($account['name2']!=''){
        	pdf_show_xy($pdf, $account['name2'], intval((216-pdf_stringwidth($pdf, $account['name2'], $font2, $s+2))/2)+53, $h=$h-10);
        	}
        pdf_setfont($pdf, $font1, $s-2);
        pdf_show_xy($pdf, $account['address1'], intval((216-pdf_stringwidth($pdf, $account['address1'], $font1, $s-2))/2)+53, $h=$h-10);
        if($account['address2']!=''){
	        pdf_show_xy($pdf, $account['address2'], intval((216-pdf_stringwidth($pdf, $account['address2'], $font1, $s-2))/2)+53, $h=$h-9);
        	}
        pdf_show_xy($pdf, $account['city'].", ".$account['state']." ".$account['zip'], intval((216-pdf_stringwidth($pdf, $account['city'].", ".$account['state']." ".$account['zip'], $font1, $s-2))/2)+53, $h=$h-9);
        if($account['country']!=''){
	        pdf_show_xy($pdf, $account['country'], intval((216-pdf_stringwidth($pdf, $account['country'], $font1, $s-2))/2)+53, $h=$h-9);
        	}
        	
        pdf_setfont($pdf, $font2, $s-2); 
        pdf_show_xy($pdf, $account['bank_name'], intval((124-pdf_stringwidth($pdf, $account['bank_name'], $font2, $s-2))/2)+332, $h=765);
        pdf_setfont($pdf, $font1, $s-2);
        pdf_show_xy($pdf, $account['bank_address'], intval((124-pdf_stringwidth($pdf, $account['bank_address'], $font1, $s-2))/2)+332, $h=$h-10);
        if($account['bank_address2']!=''){
	        pdf_show_xy($pdf, $account['bank_address2'], intval((124-pdf_stringwidth($pdf, $account['bank_address2'], $font1, $s-2))/2)+332, $h=$h-9);
        	}
        pdf_show_xy($pdf, $account['bank_city'].", ".$account['bank_state']." ".$account['bank_zip'], intval((124-pdf_stringwidth($pdf, $account['bank_city'].", ".$account['bank_state']." ".$account['bank_zip'], $font1, $s-2))/2)+332, $h=$h-9);
        if($account['bank_country']!=''){
	        pdf_show_xy($pdf, $account['bank_country'], intval((124-pdf_stringwidth($pdf, $account['bank_country'], $font1, $s-2))/2)+332, $h=$h-9);
        	}
        	
       	pdf_setfont($pdf, $font1, $s+5);
	pdf_show_xy($pdf, $check['check_number'], 583-pdf_stringwidth($pdf, $check['check_number'], $font1, $s+5), 759);
        	
	pdf_setfont($pdf, $font3, $s);
	pdf_show_xy($pdf, $d1, 495+intval((93-pdf_stringwidth($pdf, $d1, $font3, $s))/2), 727);
	
	pdf_setfont($pdf, $font4, $s+3);
	pdf_show_xy($pdf, $check['first_name']." ".$check['last_name'], 99, 692);
	
	pdf_setfont($pdf, $font1, $s+3);
	pdf_show_xy($pdf, '$', 485, 690);
	
	pdf_setfont($pdf, $font4, $s+2);
	pdf_show_xy($pdf, $check_sum_number, 495+intval((93-pdf_stringwidth($pdf, $check_sum_number, $font4, $s+2))/2), 691);
	
	pdf_setfont($pdf, $font5, $s+1);
	while(pdf_stringwidth($pdf, $check_sum_word, $font5, $s+1) < 460)
	{
		$check_sum_word=$check_sum_word.'*';
	}
	pdf_show_xy($pdf, $check_sum_word, 44, 667);
	
	pdf_setfont($pdf, $font2, $s);
	pdf_show_xy($pdf, $check['first_name']." ".$check['last_name'], 79, $h=644);
	pdf_setfont($pdf, $font1, $s);
	pdf_show_xy($pdf, $check['address1'], 79, $h=$h-10);
	if($check['address2']!=''){

	pdf_show_xy($pdf, $check['address2'], 79, $h=$h-9);
	}
	pdf_show_xy($pdf, $check['city'].", ".$check['state']." ".$check['zip'], 79, $h=$h-9);
	pdf_show_xy($pdf, $check['country'], 79, $h=$h-9);
	
	pdf_setfont($pdf, $font3, $s-1);
	pdf_show_xy($pdf, $check['memo']." comission for ".$d2, 61, 585);
	
	
	pdf_setfont($pdf, $font6, $s);
	pdf_show_xy($pdf, $bank_line_code, 201, 561);	

}
function generate_pdf($account, $checks_array){
	if (count($checks_array)%3 == 0) {
		$pages=count($checks_array)/3;
	} else {
		$pages=intval(count($checks_array)/3)+1;
	}
	$pdf=new FPDF('P', 'mm', array(612,792));
	//pdf_open_file($pdf,"");
	$h=750;
	for($i=0; $i<$pages; $i++){
		//pdf_begin_page($pdf, 612, 792);
		$pdf->AddPage();
		for($j=0; $j<3; $j++) {
				switch ($j) {
				case 0:
				    $pdf->SetXY(0, 0);
				    break;
				case 1:
				    $pdf->SetXY(0, -250);
				    break;
				case 2:
				    $pdf->SetXY(0, -250);
				    break;
		}	
			if($checks_array[$i*3+$j])
			generate_check($pdf, $account, $checks_array[$i*3+$j]);
		}
		//pdf_end_page($pdf);
	}
	$pdf->Output('invoice.pdf', 'I');
}
generate_pdf($account, $checks_array);
?>

